﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.ServiceModel.Activation;
using System.Web.UI.MobileControls;
using DataAccessLayer;
using DTO;
using IndexMarkWcfService.Caches;

namespace IndexMarkWcfService
{

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public partial class IndexMarkService : IIndexMarkService
    {
        [Cache(typeof(OrganizationCache))]
        public IEnumerable<DTO.Organization> GetOrganizations()
        {
            IDataProvider context = ProviderFactory.CreateProvider();
            Contract.Assert(context != null);
            return context.GetOrganizations();
        }

        [Cache(typeof(PeriodCache))]
        public IEnumerable<DTO.Period> GetPeriods()
        {
            IDataProvider context = ProviderFactory.CreateProvider();
            Contract.Assert(context != null);
            return context.GetPeriods();
        }

        [Cache(typeof(TermCache))]
        public IEnumerable<DTO.Term> GetTerms()
        {
            IDataProvider context = ProviderFactory.CreateProvider();
            Contract.Assert(context != null);
            return context.GetTerms();
        }

        [Cache(typeof(SectionTreeCache))]
        public IEnumerable<DTO.Section> GetTree(int IDTerm)
        {
            IDataProvider context = ProviderFactory.CreateProvider();
            Contract.Assert(context != null);
            return context.GetSectionForTree(IDTerm).ToList();
        }

        public IEnumerable<DTO.RespondentAnswers> GetRespAnswers(int IDCharTerm, Nullable<int> IDDep)
        {
            IDataProvider context = ProviderFactory.CreateProvider();
            var answers = SecurityController.Instance.GetVisibilityAnswers(
                context.GetRespAnswers(IDCharTerm, IDDep).ToList(), IDCharTerm);
            return answers;
        }

        public DTO.RespondentAnswers GetAnswersByResp(int IDCharTerm, int RespID)
        {
            IDataProvider context = ProviderFactory.CreateProvider();
            return context.GetAnswersByCharAndResp(IDCharTerm, RespID);
        }

        public IEnumerable<DTO.Gradation> GetGradations(int CharTermID)
        {
            IDataProvider context = ProviderFactory.CreateProvider();
            return context.GetGradations(CharTermID).ToList();
        }

        public void SaveAnswers(DTO.RespondentAnswers RA)
        {
            if (RA.Answers.Count() > 0)
            {
                IDataProvider context = ProviderFactory.CreateProvider();
                int IDCharTerm = RA.IDCharTerm;
                int IDResp = RA.Resp.IDRespondent;
                if (SecurityController.Instance.HasSaveAnswers(IDCharTerm, IDResp))
                {
                    context.SaveAnswers(RA, SecurityController.Instance.GetUserName());
                }
                IndexCache.Clear();
            }
        }

        public DTO.User GetUser(int IDTerm)
        {
            //запись в лог входов
            SecurityController.Instance.AddInputLog();
            //-------------------
            DTO.User User = new DTO.User
            {
                Name = SecurityController.Instance.GetUserName(),
                Roles = SecurityController.Instance.GetRoleList(),
                IsSupervisor = SecurityController.Instance.IsSupervisor(),
                Permissions = SecurityController.Instance.GetPermissions(IDTerm),
                SID = SecurityController.Instance.UserSID,
                Domain = "ugtu.net"
            };

            Contract.Assert(!String.IsNullOrEmpty(User.Name));
            Contract.Assert(User.IsSupervisor != null);
            Contract.Assert(User.Roles.Count() != 0);

            return User;
        }

        public IEnumerable<DTO.Department> GetDepartmentsList(int IDTerm)
        {
            IDataProvider context = ProviderFactory.CreateProvider();

            return context.GetDepartmentsList(IDTerm); ;
        }

        [Cache(typeof(IndexCache))]
        public IEnumerable<DTO.IndexRow> GetIndexes(int TermID, IEnumerable<int> Cats,
            IEnumerable<int> Chars, IEnumerable<int> Respondents, DTO.IndexListType Type)
        {
            IDataProvider context = ProviderFactory.CreateProvider();
            return context.GetIndexes(TermID, Cats, Chars, Respondents, Type, IndexStrategyType.PPS);
        }

        [Cache(typeof(IndexCache))]
        public IEnumerable<DTO.IndexRow> GetIndexes2(int TermID, IEnumerable<int> Cats,
            IEnumerable<int> Chars, IEnumerable<int> Respondents,
            DTO.IndexListType Type, DTO.IndexStrategyType Strategy)
        {
            IDataProvider context = ProviderFactory.CreateProvider();
            return context.GetIndexes(TermID, Cats, Chars, Respondents, Type, Strategy).ToArray();
        }

        //[Cache(typeof(IndexCache))]
        public IEnumerable<DTO.IndexRow> GetIndexesDoc(int TermID, IEnumerable<int> Cats,
            IEnumerable<int> Chars, IEnumerable<int> Respondents)
        {
            IDataProvider context = ProviderFactory.CreateProvider();
            var result = context.GetIndexes(TermID, Cats, Chars, Respondents, DTO.IndexListType.Compact, DTO.IndexStrategyType.Doctor).ToArray();
            return result;
        }

        [Cache(typeof(CategoryCache))]
        public IEnumerable<DTO.Category> GetCategories(int TermID)
        {
            IDataProvider context = ProviderFactory.CreateProvider();
            return context.GetCategories(TermID);
        }

        [Cache(typeof(DepartmentTreeCache))]
        public IEnumerable<DTO.Department> GetUSTUTree(int TermID)
        {
            IDataProvider context = ProviderFactory.CreateProvider();
            return context.GetUSTUTree(TermID);
        }

        [Cache(typeof(SectionLiteTreeCache))]
        public IEnumerable<DTO.Section> GetISTree(int TermID)
        {
            IDataProvider context = ProviderFactory.CreateProvider();
            return context.GetISTree(TermID);
            return null;
        }

        public IEnumerable<DTO.Reports.VolumePPS> GetOrderedVolums(int IDCharGrad)
        {
            IDataProvider context = ProviderFactory.CreateProvider();
            IEnumerable<DTO.Reports.VolumePPS> volums = SecurityController.Instance.GetVisibilityVolums(context.GetOrderedVolums(IDCharGrad).ToList(), IDCharGrad);
            return volums;

        }

        public IEnumerable<DTO.Reports.VolumsPPS> GetVolumsPPS(int TermID, IEnumerable<int> Respondents, IEnumerable<int> Cats, IEnumerable<int> Sections)
        {
            IDataProvider context = ProviderFactory.CreateProvider();
            IEnumerable<DTO.Reports.VolumsPPS> Volums = context.GetVolumsPPS(TermID, Respondents, Cats, Sections);
            List<DTO.Reports.VolumsPPS> list = new List<DTO.Reports.VolumsPPS>();

            foreach (DTO.Reports.VolumsPPS VPPS in Volums)
            {
                list.Add(SecurityController.Instance.GetVisibilityVolums(VPPS));
            }

            return list;
        }

        //public IEnumerable<DTO.QuestionablyValue> GetQuestionablyValues(int TermID, IEnumerable<int> Respondents, IEnumerable<int> Cats, IEnumerable<int> Sections, int PersentDeviation)
        //{
        //    IDataProvider context = ProviderFactory.CreateProvider();
        //    return context.GetQuestionablyValues(TermID, Respondents, Cats, Sections, PersentDeviation);
        //}

        public IEnumerable<DTO.Reports.NullChar> GetNullableChars(int TermID)
        {
            IDataProvider context = ProviderFactory.CreateProvider();
            return context.GetNullableChars(TermID);
        }

        public IEnumerable<DTO.Money> GetPremiums(int TermID)
        {
            IDataProvider context = ProviderFactory.CreateProvider();
            return context.GetPremiums(TermID, IndexStrategyType.PPS);
        }
        public IEnumerable<DTO.Money> GetPremiums2(int TermID, DTO.IndexStrategyType Strategy)
        {
            IDataProvider context = ProviderFactory.CreateProvider();
            return context.GetPremiums(TermID, Strategy);
        }

        //public IEnumerable<DTO.Department> GetFacultyList(int TermID)
        //{
        //    IDataProvider context = ProviderFactory.CreateProvider();
        //    return context.GetFacultyList(TermID);
        //}

        public IEnumerable<DTO.Position> GetPositionList()
        {
            IDataProvider context = ProviderFactory.CreateProvider();
            return context.GetPositionList();
        }

        public IEnumerable<DTO.Section> GetParentsSections(int TermID)
        {
            IDataProvider context = ProviderFactory.CreateProvider();
            List<DTO.Section> list = context.GetParentsSections(TermID).ToList();
            return list;
        }

        public IEnumerable<DTO.IndexRow> GetLIDIA(int TermID, int[] RespID)
        {
            IDataProvider context = ProviderFactory.CreateProvider();
            return context.GetLIDIA(TermID, RespID);
        }

    }
}